@isTest

private class testHCPEducation_AttendeeTrigger {

    static testMethod void testcountAttendeeSaloonTrigger() {
         String rtid;
         String hcprtid;
         RecordType rt=new recordtype();
         RecordType hcprt=new recordtype();
         hcprt=[select id from recordtype where sobjecttype=:'HCPEducation__c' and name=:'学术沙龙'];
         rt=[select id from recordtype where sobjectType=:'NationalAttendee__c' and name=:'学术沙龙']; 
         hcprtid=hcprt.id;
         hcprtid=hcprtid.substring(0,15);
         rtid=rt.id;
        rtid=rtid.substring(0,15);
        Profile p  = [select id from profile where name=: 'MSFE Sales Effectiveness'];   //id =:UserInfo.getProfileId()];      
         User u3 =new User (alias = 'new', email = 'shlia@isoftstone.com', emailencodingkey='UTF-8', lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', profileid = p.id, timezonesidkey = 'Asia/Shanghai', username='new3@testorg1.com');
        insert u3;
        User u2 =new User (alias = 'new', email = 'shlia@isoftstone.com', emailencodingkey='UTF-8', lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', profileid = p.id, timezonesidkey = 'Asia/Shanghai', username='new@testorg1.com',managerid=u3.id);
        insert u2;
        
        User u1 =new User (alias = 'standt', email = 'shlia@isoftstone.com', emailencodingkey='UTF-8', lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', profileid = p.id, timezonesidkey = 'Asia/Shanghai', username='standarduser@testorg1.com',managerid=u2.id);
        insert u1;
        
       System.runAs(u1) {
           hospital__c hp=new hospital__c(name='testhospital',Nickname__c='testnick', approvalstatus__c='审批通过',active__c=true);
           insert hp;
          
           HCP__c hcp1=new hcp__c(Name='testhcp1',Hospital__c=hp.id,Active__c=true,class2__c='决策人(DM)');
           insert hcp1;
           HCP__c hcp2=new hcp__c(Name='testhcp2',Hospital__c=hp.id,Active__c=true,class2__c='决策人(DM)');
           insert hcp2;
          
           HCPEducation__c hcpedu=new HCPEducation__c(name='testsaloon',date__c=date.today()+1 ,recordtypeid=hcprtid,woc__c=null,noc__c=null,eoc__c=null,soc__c=null,place__c='asdf',organizer__c='其他',ifotherorganizer__c='aaa');             //create a saloon type record;
           insert hcpedu;
          
          
           NationalAttendee__c na1=new NationalAttendee__c(HCPEducation__c=hcpedu.id,HCP__c=hcp1.id,RecordTypeid=rtid,IsAttended__c=true);
           NationalAttendee__c na2=new NationalAttendee__c(HCPEducation__c=hcpedu.id,HCP__c=hcp2.id,RecordTypeid=rtid,IsAttended__c=true);
           insert na1;
           insert na2;
           update na1;
           update na2;
           delete na1;
           delete na2;
           delete hcp1;
           delete hcp2;
           delete hp;
           
           hcpedu.date__c = hcpedu.date__c + 1;
           update hcpedu;
           //delete hcpedu; // fix by scao
     }
    }
    
    
    
      static testMethod void testcountAttendeePSTrigger() {
         String rtid;
         String hcprtid;
         RecordType rt=new recordtype();
         RecordType hcprt=new recordtype();
         hcprt=[select id from recordtype where sobjecttype=:'HCPEducation__c' and name=:'科内会'];
         rt=[select id from recordtype where sobjectType=:'NationalAttendee__c' and name=:'科内会']; 
         hcprtid=hcprt.id;
         hcprtid=hcprtid.substring(0,15);
         rtid=rt.id;
        rtid=rtid.substring(0,15);
        Profile p  = [select id from profile where name=: 'MSFE Sales Effectiveness'];   //id =:UserInfo.getProfileId()];      
        User u1 =new User (alias = 'standt', email = 'shlia@isoftstone.com', emailencodingkey='UTF-8', lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', profileid = p.id, timezonesidkey = 'Asia/Shanghai', username='standarduser@testorg1.com');
    //   System.assert(rtid =='012O00000008UfL',rtid);
        insert u1;
        
       System.runAs(u1) {
           hospital__c hp=new hospital__c(name='testhospital',Nickname__c='testnick', approvalstatus__c='审批通过',active__c=true);
           insert hp;
          
           HCP__c hcp1=new hcp__c(Name='testhcp1',Hospital__c=hp.id,Active__c=true,class2__c='决策人(DM)');
           insert hcp1;
           HCP__c hcp2=new hcp__c(Name='testhcp2',Hospital__c=hp.id,Active__c=true,class2__c='决策人(DM)');
           insert hcp2;
          
           HCPEducation__c hcpedu=new HCPEducation__c(name='testsaloon',date__c=date.today()+10 ,recordtypeid=hcprtid,woc__c=null,noc__c=null,eoc__c=null,soc__c=null,place__c='asdf',organizer__c='其他',ifotherorganizer__c='aaa');             //create a saloon type record;
           insert hcpedu;
          
           NationalAttendee__c na1=new NationalAttendee__c(HCPEducation__c=hcpedu.id,HCP__c=hcp1.id,RecordTypeid=rtid,IsAttended__c=true);
           NationalAttendee__c na2=new NationalAttendee__c(HCPEducation__c=hcpedu.id,HCP__c=hcp2.id,RecordTypeid=rtid,IsAttended__c=true);
           insert na1;
           insert na2;
           update na1;
           update na2;
          // delete na1;
           //delete na2;
           //delete hcp1;
           //delete hcp2;
           delete hp;
           delete hcpedu; //fix by scao
     }
    }
    
      static testMethod void testcountAttendeeNationalTrigger() {
         String rtid;
         String hcprtid;
         RecordType rt=new recordtype();
         RecordType hcprt=new recordtype();
         hcprt=[select id from recordtype where sobjecttype=:'HCPEducation__c' and name=:'全国医护人员教育活动'];
         rt=[select id from recordtype where sobjectType=:'NationalAttendee__c' and name=:'全国医护人员教育活动']; 
         hcprtid=hcprt.id;
         hcprtid=hcprtid.substring(0,15);
         rtid=rt.id;
        rtid=rtid.substring(0,15);
        UserRole ur2 = [select id From UserRole where name = 'SOC1 GZ Sales Rep-M3'];
        Profile p  = [select id from profile where name=: 'MSFE Sales Effectiveness'];   //id =:UserInfo.getProfileId()];      
        User u1 =new User (alias = 'standt', email = 'shlia@isoftstone.com', emailencodingkey='UTF-8', lastname = 'Testing',UserRoleId = ur2.id, languagelocalekey = 'en_US', localesidkey='en_US', profileid = p.id, timezonesidkey = 'Asia/Shanghai', username='standarduser@testorg1.com');
    
        insert u1;
        
        User u2 =new User (alias = 'standrt', email = 'shlira@isoftstone.com', emailencodingkey='UTF-8', lastname = 'Testaing',UserRoleId = ur2.id, languagelocalekey = 'en_US', localesidkey='en_US', profileid = p.id, timezonesidkey = 'Asia/Shanghai', username='strduser@testorg1.com');
    
        insert u2;        
    
        Profile p1  = [select id from profile where name = 'MSFE Sales Effectiveness'];
        UserRole ur1 = [select id From UserRole where name = 'SOC1 GZ Sales Supervisor-M2'];
        User ue = new User (alias = 'stafndt', email = 'shuaicao@isoftstone.com', emailencodingkey='UTF-8', UserRoleId = ur1.id,
                lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', Title__c = 'Sales Rep',profileid = p1.id, 
                timezonesidkey = 'America/Los_Angeles', username='standargduser@testorg1.com');
        insert ue; 

        
      System.runAs(u1) { 
           hospital__c hp=new hospital__c(ownerid = u1.id,name='testhospital',Nickname__c='testnick', approvalstatus__c='审批通过',active__c=true);        
          insert hp;
             
           HCP__c hcp1=new hcp__c(Name='testhcp1',Hospital__c=hp.id,Active__c=true,class2__c='决策人(DM)');
           insert hcp1;
           HCP__c hcp2=new hcp__c(Name='testhcp2',Hospital__c=hp.id,Active__c=true,class2__c='决策人(DM)');
           insert hcp2;
        
           HCPEducation__c hcpedu=new HCPEducation__c(ownerid = ue.id,name='testsaloon',date__c=date.today()+1,recordtypeid=hcprtid,NOC__c=20,soc__c=20,eoc__c=20,woc__c=20,place__c='asdf',organizer__c='其他',ifotherorganizer__c='aaa');             //create a saloon type record;
           insert hcpedu;
           recordtype rgrt=[select id from recordtype where sobjecttype=:'place__c' and name=:'region'];
           Place__c rgl=new  Place__c();
           rgl.name='华南区';
           rgl.recordtypeid=rgrt.id;
           rgl.ownerid = u1.id;
           insert rgl;
        
           RegionNameList__c rnl=new RegionNameList__c(ownerid = u1.id,HCPEducation__c=hcpedu.id,name='华南区名单',region__c=rgl.id,TargetNo__c=20);
           insert rnl;
           
           citynamelist__c cnl=new citynamelist__c(ownerid = u1.id,RegionNameList__c=rnl.id,name='广州',TargetNo__c=20);
           insert cnl;
           List<RegionNameList__c> place=new list<RegionNameList__c>();
     
              Place__c rgl1=new  Place__c();
              rgl1.name='华南区';
               rgl1.ownerid = u1.id;
              rgl1.recordtypeid=rgrt.id;
           insert rgl1;
            RegionNameList__c rnl1=new RegionNameList__c(ownerid = u1.id,HCPEducation__c=hcpedu.id,name='华南区名单',region__c=rgl1.id,TargetNo__c=20);
           insert rnl1;
           place=[select id from RegionNameList__c where name=:'华南区名单' and hcpeducation__c=:hcpedu.id];
           citynamelist__c cnl1=new citynamelist__c(ownerid = u1.id,regionnamelist__c=place[0].id,name='广州',targetno__C=20);
           insert cnl1;
          
         Place__c rgl2=new  Place__c();
              rgl2.name='华东区';
              rgl2.recordtypeid=rgrt.id;
              rgl2.ownerid = u1.id;
           insert rgl2;
            RegionNameList__c rnl2=new RegionNameList__c(ownerid = u1.id,HCPEducation__c=hcpedu.id,name='华东区名单',region__c=rgl2.id,TargetNo__c=20);
           insert rnl2;
           place=[select id from RegionNameList__c where name=:'华东区名单' and hcpeducation__c=:hcpedu.id];
           citynamelist__c cnl2=new citynamelist__c(ownerid = u1.id,regionnamelist__c=place[0].id,name='长沙',targetno__C=20);
           insert cnl2;
           
           // rgl=[select id from  Place__c where name=:'华西区'];
           Place__c rgl3=new  Place__c();
              rgl3.name='华西区';
              rgl3.recordtypeid=rgrt.id;
              rgl3.ownerid = u1.id;
           insert rgl3;
            RegionNameList__c rnl3=new RegionNameList__c(ownerid = u1.id,HCPEducation__c=hcpedu.id,name='华西区名单',region__c=rgl3.id,TargetNo__c=20);
           insert rnl3;
           place=[select id from RegionNameList__c where name=:'华西区名单' and hcpeducation__c=:hcpedu.id];
           citynamelist__c cnl3=new citynamelist__c(ownerid = u1.id,regionnamelist__c=place[0].id,name='dlkj',targetno__C=20);
           insert cnl3;
          
           JoinPeople__c jp = new JoinPeople__c ();
           jp.CityNameList__c = cnl.Id;
           jp.User__c = u2.Id;
           insert jp;
         System.runAs(u2) {    
           NationalAttendee__c na1=new NationalAttendee__c(ownerid = u2.id,HCPEducation__c=hcpedu.id,HCP__c=hcp1.id,RecordTypeid=rtid,IsAttended__c=true,CityNameLists__c=cnl.id,RegionNameList__c=rnl.id);
           NationalAttendee__c na2=new NationalAttendee__c(ownerid = u2.id,HCPEducation__c=hcpedu.id,HCP__c=hcp2.id,RecordTypeid=rtid,IsAttended__c=true,CityNameLists__c=cnl.id,RegionNameList__c=rnl.id);
           insert na1;
           insert na2;
           update na1;
           update na2;
           
        }
        
     }
    }
}